# 
# To change this template, choose Tools | Templates
# and open the template in the editor.

require 'rexml/document' 
 
module PCLink
  
  class NoSuchContent < Exception 
  end 
  
  class ContentData
    
    attr_reader :channel
    
    def initialize(element)
      @properties = {}
      @element = element
      element.children.each do |child| 
        @properties[child.name.to_sym] = child.text
      end
    end

    def channel=(chan)
      @channel = chan
    end
    
    def method_missing(method_id, *arguments)
      return @properties[method_id.to_sym] if @properties.has_key?(method_id.to_sym)
      raise NoSuchContent, "no such content: #{method_id}"
    end
  
    def to_xml
      s = String.new
      @element.write(s, 2)
    end 
    
    def to_s
      s = "["
      @properties.keys.each {|k| s = s + "#{k}:#{@properties[k]}"}
      s + "]"
    end
    
    def self.from_element(el)
      name = el.elements['name'].text
      nodeid = el.elements['nodeid'].text.to_i
      parentid = el.elements['parentid'].text.to_i
      self.new(el)
    end
  end

end